<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="chkAll">
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model="task"
      @keydown.enter="sub"
    />
  </header>
</template>

<script>
export default {
  props: ['list'],
  data () {
    return {
      task: ''
    }
  },
  methods: {
    sub () {
      if (this.task.trim().length === 0) return alert('请输入任务')
      const id = this.list.length ? this.list[this.list.length - 1] + 1 : 100
      this.list.push({ id, name: this.task, isDone: false })
      this.task = ''
    }
  },
  computed: {
    chkAll: {
      get () {
        return this.list.length ? this.list.every(el => el.isDone) : false
      },
      set (checked) {
        this.list.forEach(el => (el.isDone = checked))
      }
    }
  }
}
</script>
